home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dtk_demo.zip / NWEEKDAY.C < prev    next >
C/C++ Source or Header  |  1991-09-12  |  2KB  |  100 lines

  1. /*  NWEEKDMN.C
  2.  *  calculates the date of the nth Tuesday,
  3.  *  or whatever, in a given month
  4.  *  last mod.: 12-AUG-91
  5.  */
  6.  
  7. #include <STDIO.H>
  8. #include <STDLIB.H>
  9. #include <STRING.H>
  10.  
  11. #include <L_DATE.H>
  12. #include <L_STR.H>
  13.  
  14. char *usage = "\nUse: NWEEKDAY n day_name month year"
  15. "\nThis gives the date of the nth Friday or whatever"
  16. "\nfor the specified month and year in the Gregorian calendar."
  17. "\nThe day name can be abbreviated, e.g. NWEEKDAY 3 Fri 12 2012.";
  18.  
  19. Uchar weekday[20];
  20. Uchar date_str[64];
  21.  
  22. /*----------------------------*/
  23. void main(int argc, char **argv)
  24. {
  25. int n, dow, specified_dow;
  26. Date date;
  27. Date_format df;  /*  for Date and Date_format see L_AUXSTR.H  */
  28.  
  29. if ( argc < 5 )
  30.     {
  31.     printf(usage);
  32.     exit(0);
  33.     }
  34.  
  35. n = atoi(argv[1]);
  36. strcpy(weekday,argv[2]);
  37. strlwr(weekday);
  38. capitalize(weekday,0);
  39. date.day = 1;
  40. date.month = atoi(argv[3]);
  41. date.year = atol(argv[4]);
  42.  
  43. if ( n < 1 || n > 5 )
  44.     {
  45.     printf("\nInvalid n parameter.\n");
  46.     exit(1);
  47.     }
  48.  
  49. if ( !date_valid(&date) )
  50.     {
  51.     printf("\nInvalid month.\n");
  52.     exit(2);
  53.     }
  54.  
  55. /*  convert name of day of week to an integer
  56.  *  0 = Sunday, 6 = Saturday
  57.  */
  58.  
  59. dow = 0;
  60. while ( dow < 7 && !strstr((Str_ptr)day_name(dow),weekday) )
  61.     dow++;
  62.  
  63. if ( dow > 6 )
  64.     {
  65.     printf("\nName of weekday is invalid.\n");
  66.     exit(3);
  67.     }
  68. else
  69.     specified_dow = dow;
  70.  
  71. /*  calculate day of week of 1st day of month/year  */
  72. dow = day_of_week(&date);
  73.  
  74. while ( dow != specified_dow )
  75.     {
  76.     dow = (++dow)%7;
  77.     date.day++;
  78.     }
  79.  
  80. /*  date now contains the date of
  81.  *  the 1st such weekday in the month
  82.  */
  83.  
  84. date.day += 7*(n-1);
  85. if ( date_valid(&date) )
  86.     {
  87.     set_date_format_default(&df);
  88.     df.commas_in_year = TRUE;
  89.     df.short_year = TRUE;
  90.     date_to_str(&date,&df,date_str);
  91.     printf("\nThe %d%s %s of the %d%s month of the year %ld is %s.\n",
  92.         n,ordinal_suffix(n),day_name(dow),date.month,
  93.         ordinal_suffix(date.month),date.year,date_str);
  94.     }
  95. else
  96.     printf("\nThere is no %d%s %s of the %d%s month of the year %ld.\n",
  97.         n,ordinal_suffix(n),day_name(dow),date.month,
  98.         ordinal_suffix(date.month),date.year);
  99. }
  100.